<?php
/**
 * Created by PhpStorm.
 * User: hao03
 * Date: 2019/2/15
 * Time: 20:52
 */

namespace app\controller;

use rap\config\Config;
use rap\web\Request;
use rap\web\Response;
use app\domain\car;
use app\domain\user;

class CarInfoController
{
    public function index() {

        return 'carInfo/index';
    }
    /**
     * 添加车辆信息
     * @param Request $request
     * @return array
     */
    public function addDrivingInfo (Request $request) {
        //判断是否是POST请求
        if ($request->method() == "POST") {
            $token = $request->param("token", 0);
            //获取参数
            $driving_img = $request->param('driving_img');//行车证
            $jiazhao_img1 = $request->param('jiazhao_img1');//驾驶证
            $jiazhao_img2 = $request->param('jiazhao_img2');//驾驶证
            $name = $request->param('name', '');//姓名
            $phone = $request->param('phone', '');//手机号
            $id_card = $request->param('id_card', '');//身份证号
            $car_name = $request->param('car_name', '');//车名称
            $car_set = $request->param('car_set', '');//车系
            $car_model = $request->param('car_model', '');//车型

            //判断驾驶证已经行驶证信息是否存在
            if (!$driving_img) return CommonController::false(401, '请上传行车证图片信息', []);
            if (!$jiazhao_img1) return CommonController::false(401, '请上传驾照图片信息', []);
//            if (!$jiazhao_img2) return CommonController::false(401, '请上传驾照图片信息', []);
            if (!$name) return CommonController::false(401, '请填写车主姓名', []);
//            if (!$id_card) return CommonController::false(401, '请填写身份证信息', []);
//            if (!$phone) return CommonController::false(401, '请填写手机号', []);
            if (!$car_name) return CommonController::false(401, '请填写汽车名称号', []);
            if (!$car_set) return CommonController::false(401, '请上传车系信息', []);
            if (!$car_model) return CommonController::false(401, '请上传车型信息', []);

//            if (CommonController::checkUserPhone($phone) == 0) return CommonController::false(402, '手机号不正确', []);
//            if (CommonController::checkUserIdCard($id_card) == 0) return CommonController::false(402, '身份证不正确', []);

            if (!$_SESSION[$token]['user_id']) {
                $res = CommonController::getUserInfo($token);
                CommonController::getPersonalInfo($res, $token);

            }

//            判断车辆信息是否已经添加
            $where = [
                'user_id' => $_SESSION[$token]['user_id'],
                'car_name' => $car_name,
                'car_set' => $car_set,
                'car_model' => $car_model,
                'isdelete' => 0
            ];
            $field = "*";
            //获取添加结果
            $res = car::getUserCarInfo($where, $field);
            //判断添加结果
            if ($res) {
                return CommonController::false(403, '车辆信息已存在，请勿重复添加，若要继续添加，请联系客服', []);
            }

            //驾驶证以及行车证信息识别
            $driving_info = CommonController::getOcrData($driving_img, "driving");
            //获取驾照信息
            $jiazhao_info1 = CommonController::getOcrData($jiazhao_img1, "jiazhao");
//            $jiazhao_info2 = CommonController::getOcrData($jiazhao_img2,'jiazhao');
            //获取行车证添加结果
            $res_dr = car::addUserDrivingInfo($driving_info, $car_name, $car_set, $car_model, $_SESSION[$token]['phone'], $id_card, $name, $driving_img, $jiazhao_img1, $jiazhao_img2, $_SESSION[$token]['user_id'], $_SESSION[$token]['head']);

            if (!$res_dr) {
                throw new MsgException("添加行车证信息失败");
            }
            //获取驾照添加结果
            $res_jz = car::addUserJzInfo($_SESSION[$token]['user_id'], $jiazhao_info1);
            if (!$res_jz) {
                throw new MsgException('添加驾照信息失败');
            }

            return CommonController::success('添加成功', []);
        }

    }

    /**
     * 获取用户单条车辆信息
     * @param Request $request
     * @return array
     */
    public function getUserCarInfo(Request $request) {

        if ($request->method() == "POST") {
            $id = $request->param('id', '');
            $token = $request->param('token', '');
            //查询条件
            $where = [
                'id' => $id,
                'is_del' => 0
            ];
            ///返回所需字段
            $field = '*';
            //获取用户所有车辆信息
            $data = car::getUserCarInfos($where, $field);
            //判断数据请求的数据是否存在
            if ($data) return CommonController::success("获取数据成功",$data);
            else return CommonController::false(401, "暂无数据", []);
        }
    }

    /**
     * 获取用户所有车辆信息
     * @param Request $request
     * @return array
     */
    public function getUserCarInfoList(Request $request) {

        if ($request->method() == "POST") {
            $token = $request->param("token");

//            //判断是否存在
            if (!$_SESSION[$token]['user_id']) {
                $res = CommonController::getUserInfo($token);
                $data = CommonController::getPersonalInfo($res, $token);

            }
            //查询条件
            $where = [
                'user_id' => $_SESSION[$token]['user_id'],
//                'user_id' => 3163,
                'status' => 1,
                'isdelete' => 0
            ];
            ///返回所需字段
            $field = 'id, user_head, car_name, car_set, car_model, status, car_code, engine_code, class_code, car_pic';
            //获取用户所有车辆信息
            $data = car::getUserCarInfos($where, $field);
            //判断数据请求的数据是否存在

            if ($data) return CommonController::success("获取数据成功",$data);
            else return CommonController::false(404, "暂无数据", []);
        }
    }

    /**
     * 删除用户车辆信息
     * @param Request $request
     * @return array
     */
    public function deleteUserCarInfo (Request $request) {
        if ($request->method() == "POST") {

            $id = $request->param('id', 0);
            if (!$id) return CommonController::false(401, '参数不能为空', []);
            //查询条件
            $where = [
                'id' => $id,
                'is_del' => 0
            ];

            $res = car::deleteUserCarInfo($where);
            //判断数据请求的数据是否存在
            if ($res) return CommonController::success("获取数据成功",[]);

            else return CommonController::false(401, "暂无数据", []);
        }
    }

    public function getToken () {

        $res = CommonController::getQNToken();


        if ($res) {
            return CommonController::success('获取数据成功',['token' => $res]);
        } else {
            return CommonController::false(404, '获取信息失败', []);
        }

    }

    /**
     * @return bool|mixed|string
     */
    public function modeUserGold() {


        $res = CommonController::modifyGold(10000);
        return $res;
    }

    /**
     * @param Request $request
     * @return array
     */
    public function getOilPrice(Request $request) {
        if ($request->method() == "POST" || $request->method() == "GET"){
            $province_name = $request ->param("province_name", "山东");

            $list = car::getOilPrice($province_name);
//            $list = true;
            if ($list) {
                $data['92h'] = $list['92h'];
                $data['95h'] = $list['95h'];
                $data['98h'] = $list['98h'];
                $data['0h'] = $list['0h'];
//                $data['92h'] = "4.79";
//                $data['95h'] = '5.79';
//                $data['98h'] = "8.00";
//                $data['0h'] = "9999";
                return CommonController::success('获取数据成功', $data);
            } else {
                return CommonController::false(404, '暂无查询数据', []);
            }
        }
    }

    /**
     * 获取车名信息
     */
    public function getCarNameInfo (Request $request) {

        if ($request->method() == "GET") {
            $where = [
                'is_del' => 0
            ];
            $field = 'car_name, auto_home_id as car_id, bfirstletter';
            $res = car::getCarNameList($where, $field);

            if ($res) {
                return CommonController::success("获取数据成功",$res);
            } elseif($res == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    public function getCarSetInfoList (Request $request) {

        if ($request->method() == "POST") {
            $car_id = $request->param('car_id');

            $where = [
                'car_id'=> $car_id,
                'is_del' => 0
            ];
            $field = 'car_set_name, auto_home_carSet_id as set_id, set_type, set_type_name';
            $res = car::getCarSetInfoList($where, $field);

            if ($res) {
                return CommonController::success("获取数据成功",$res);
            } elseif($res == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    public function getCarModelInfoList (Request $request) {

        if ($request->method() == "POST") {

            $set_id = $request->param('set_id');

            $where = [
                'car_set_id' => $set_id,
                'is_del' => 0
            ];
            $field = "car_model_name, type_name";
            $res = car::getCarModelInfoList($where, $field);

            if ($res) {
                return CommonController::success("获取数据成功",$res);
            } elseif($res == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    public function getGetUsedCar(Request $request) {

        if ($request->method() == "POST") {

            $set_id = $request->param('set_id');

            $where = [
                'car_set_id' => $set_id,
                'is_del' => 0
            ];
            $field = "car_model_name, type_name";
            $res = car::getCarModelInfoList($where, $field);

            if ($res) {
                return CommonController::success("获取数据成功",$res);
            } elseif($res == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    /********************************************************二手车相关******************************************************************/

    /**
     * 获取二手车所在省份
     */
    public function getUserProvince (Request $request) {
        if ($request->method() == "GET" || $request->method() == "POST") {
            $config = Config::getFileConfig();

            $param = [
                'key' => $config['handCar']['key']
            ];

//            $list = car::getRequestResult($config['handCar']['provinceUrl'], $param);
//            print_r($list);
            die();
            if ($list['result']){
                return CommonController::success("获取数据成功",$list['result']);
            }elseif($list['result'] == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    /**
     * 获取二手车所在城市
     */
    public function getUserCity (Request $request) {

        if ($request->method() == "POST") {
            $city = $request->param('proID', 16);
            $config = Config::getFileConfig();

            $param = [
                'key' => $config['handCar']['key'],
                'province' => $city
            ];

//            $list = car::getRequestResult($config['handCar']['cityUrl'], $param);
//            print_r($list);
            die();
            if ($list['result']){
                return CommonController::success("获取数据成功",$list['result']);
            }elseif($list['result'] == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    /**
     *
     */
    public function getUserCarBrand (Request $request) {
        if ($request->method() == "POST") {
            $type = $request->param('type', 1);

            $vehicle = "passenger"; # 1:乘用车、2：商用车
            if ($type != 1) {
                $vehicle = 'commercial';
            }
            $config = Config::getFileConfig();

            $param = [
                'key' => $config['handCar']['key'],
                'vehicle' => $vehicle
            ];

//            $list = car::getRequestResult($config['handCar']['bandUrl'], $param);
//            print_r($list);
            die();
            if ($list['result']){
                return CommonController::success("获取数据成功",$list['result']);
            }elseif($list['result'] == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    /**
     *
     */
    public function getUserCarSet(Request $request) {
//        2000563
        if ($request->method() == "POST") {
            $brand = $request->param('brandId', 2000563);

            if (!$brand) {
                return CommonController::false(401, '参数不能为空', array());
            }
            $config = Config::getFileConfig();

            $param = [
                'key' => $config['handCar']['key'],
                'brand' => $brand
            ];

//            $list = car::getRequestResult($config['handCar']['seriesUrl'], $param);
//            print_r($list);
            die();

            if ($list['result']){
                return CommonController::success("获取数据成功",$list['result']);
            }elseif($list['result'] == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    /**
     *
     */
    public function getUserCar(Request $request) {
        //20000527
        if ($request->method() == "POST") {
            $series = $request->param('series', 20000527);

            if (!$series) {
                return CommonController::false(401, '参数不能为空', array());
            }
            $config = Config::getFileConfig();

            $param = [
                'key' => $config['handCar']['key'],
                'car' => $series
            ];

//            $list = car::getRequestResult($config['handCar']['carUrl'], $param);
//            print_r($list);
            die();

            if ($list['result']){
                return CommonController::success("获取数据成功",$list['result']);
            }elseif($list['result'] == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }

    /**
     *
     */
    public function getUserCarYear(Request $request) {
        //1091230
        if ($request->method() == "POST") {
            $carId = $request->param('carId', 1091230);

            if (!$carId) {
                return CommonController::false(401, '参数不能为空', array());
            }
            $config = Config::getFileConfig();

            $param = [
                'key' => $config['handCar']['key'],
                'car' => $carId
            ];

//            $list = car::getRequestResult($config['handCar']['yearUrl'], $param);
//            print_r($list);
            die();

            if ($list['result']){
                return CommonController::success("获取数据成功",$list['result']);
            }elseif($list['result'] == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }
        }
    }
    /**
     * 获取精准二手车价格
     */
    public function getUserUsedCarPrice (Request $request){
        if($request->method() == "POST") {
            //参数获取
            $carstatus = $request->param('carstatus', 2); //1:优秀、2.一般、3.较差
            $purpose   = $request->param("purpose", 1);//1自用 2公务商用 3营运
            $province  = $request->param("province", 16);//默认山东
            $city      = $request->param('city', 162);//默认滨州
            $carId     = $request->param('carId', 1091230);//车型id
            $carYear   = $request->param("carYear", 2017);//上牌年份
            $carMonth  = $request->param('carMonth', 11);//上牌月份
            $mileage   = $request->param('mileage', 1);
            //条件判断
            if (!$carstatus) return CommonController::false(401, '参数不能为空', []);
            if (!$purpose) return CommonController::false(401, '参数不能为空', []);
            if (!$province) return CommonController::false(401, '参数不能为空', []);
            if (!$city) return CommonController::false(401, '参数不能为空', []);
            if (!$carId) return CommonController::false(401, '参数不能为空', []);
            if (!$carYear) return CommonController::false(401, '参数不能为空', []);
            if (!$carMonth) return CommonController::false(401, '参数不能为空', []);
            if (!$mileage) return CommonController::false(401, '参数不能为空', []);

            $config = Config::getFileConfig();
            $param = [
                'carstatus' => $carstatus,
                'purpose' => $purpose,
                'province' => $province,
                'city' => $city,
                'car' => $carId,
                'useddate' => $carYear,
                'useddateMonth' => $carMonth,
                'mileage' => $mileage,
                'key' => $config['handCar']['key'],
            ];

//            $list = car::getRequestResult($config['handCar']['url'], $param);
//            print_r($list);
            die();

            if ($list['result']){
                return CommonController::success("获取数据成功",$list['result']);
            }elseif($list['result'] == false) {
                return CommonController::false(404, '暂无相关数据', array());
            } else {
                return CommonController::false(500, '服务器开小差，请稍后重试', array());
            }

        }
    }

}